<?php
namespace app\admin\controller;

use think\Db;

class Brand extends Base
{

    public function config(){
        $list = Db::table('bze_config')
            ->where(['is_show'=>1,'location'=>3])
            ->order('sort')
            ->select();
        return $this->fetch('', [
            'list'  =>  $list,
            'list2'  =>  json_encode($list),
        ]);
        return $this->fetch();
    }

    public function index() {
        return $this->fetch();
    }

    /**
     * @description : [招聘列表]
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function workIndex(){
        $list = model('Work')->select();
        return $this->fetch('', [
            'list' => $list,
        ]);
    }

    /**
     * @description : [添加招聘]
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function workAdd(){
        $id = input('param.id');
        if(!request()->isPost()){
            $work = model('Work')->where(['id'=>$id])->find();
            return $this->fetch('', [
                'work' => $work,
            ]);
        }

        $postData = input('post.');
        try{
            if(!empty($id)){
                model('Work')->save($postData, ['id'=>$id]);
            }else{
                model('Work')->save($postData);
            }
        }catch (\Exception $e){
            $this->error('添加失败。'.$e->getMessage());
        }
        $this->success('添加成功', 'brand/workIndex');
    }

    /**
     * @description ：[删除招聘]
     * @return array
     */
    public function workDelete(){
        $id = input('param.id');
        model('Work')->where(['id'=>$id])->delete();
        return show(0, '删除成功');
    }

    /**
     * @description : [联系页面]
     * @return array|mixed
     * @throws \Exception
     */
    public function linkPage(){
        if(!request()->isPost()){
            $linkConfig = model('Config')
                ->where(['config_key'=>['IN', ['LOCATION_LAT', 'LOCATION_LNG', 'LINK_INFO']]])
                ->select();
            $linkConfig2 = [];
            foreach($linkConfig AS $key=>$val){
                $linkConfig2[$val['config_key']] = $val['config_value'];
            }
            return $this->fetch('', [
                'linkConfig' => $linkConfig2,
            ]);
        }
        $postData = input('post.');
        $lat = $postData['lat'];
        $lng = $postData['lng'];
        $info = $postData['info'];
        $saveData = [
            [
                'id' => '22',
                'config_value' => $lat,
            ],
            [
                'id' => '23',
                'config_value' => $lng,
            ],
            [
                'id' => '24',
                'config_value' => $info,
            ]
        ];
        model('Config')->saveAll($saveData);
        return show(0, '编辑成功');
    }

    /**
     * @description : [内容图片]
     */
    public function contentImg(){
        return $this->fetch();
    }

    /**
     * @description : [品牌历程]
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function historyList(){
        $list = model('BrandHistory')->select();
        return $this->fetch('', [
            'list' => $list,
        ]);
    }

    /**
     * @description : [添加编辑品牌历程]
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function addEditHistory(){
        $id = input('param.id');
        if(!request()->isPost()){
            $history = model('BrandHistory')->find($id);
            return $this->fetch('', [
                'history' => $history,
            ]);
        }
        $postData = input('post.');
        if(isset($id)){
            $where['id'] = $id;
        }
        try{
            model('BrandHistory')->save($postData,$where);
        }catch (\Exception $e){
            $this->error('编辑失败。'.$e->getMessage());
        }
        $this->success('编辑成功', 'brand/historyList');
    }

    public function delHistory(){
        $id = input('param.id');
        model('BrandHistory')->where(['id'=>$id])->delete();
        return show(0, '删除成功');
    }


    public function brandCog(){
        $list = model('BrandCog')->select();
        return $this->fetch('', [
            'list' => $list,
        ]);
    }

    public function editBrandCog(){
        $id = input('param.id');
        if(!request()->isPost()){
            $cog = model('BrandCog')->find($id);
            return $this->fetch('', [
                'cog' => $cog,
            ]);
        }
        $postData = input('post.');
        try{
            model('BrandCog')->save($postData, ['id' => $id]);
        }catch (\Exception $e){
            $this->error('编辑失败。'.$e->getMessage());
        }
        $this->success('编辑成功', 'brand/brandCog');
    }

}
